package com.masterlu.leetcode.daily.number.medium;

/**
 * 编写一个程序，找出第 n 个丑数。
 * <p>
 * 丑数就是质因数只包含 2, 3, 5 的正整数
 * https://leetcode-cn.com/problems/ugly-number-ii/
 *
 * @Author：masterlu
 * @Date：2021/3/24 11:08 下午
 */
public class M264Ugly {

    public int[] nums = new int[1690];

    M264Ugly(){
        nums[0] = 1;
        int i2 = 0, i3 = 0, i5 = 0;
        for (int i = 1; i < 1690; i++) {
            int ugly = Math.min(Math.min(nums[i2] * 2, nums[i3] * 3), nums[i5] * 5);
            nums[i] = ugly;
            if (ugly== nums[i2]*2) {i2++;}
            if (ugly== nums[i3]*3) {i3++;}
            if (ugly== nums[i5]*5) {i5++;}
        }
    }

    public static void main(String[] args) {

        int i = nthUglyNumber(1352);
        System.out.println(i);

    }

    public static M264Ugly ugly = new M264Ugly();

    private static int nthUglyNumber(int n) {

        return ugly.nums[n-1];
    }



   /* public static int nthUglyNumber(int n) {

        //暴力美学
        int count = 0;
        for (int i = 1; i < Integer.MAX_VALUE; i++) {
            if (isUgly(i)) {
                count++;
            }
            if (count == n) {
                return i;
            }
        }

        return 0;
    }

    private static boolean isUgly(int i) {
        if (i < 0) {
            return Boolean.FALSE;
        }

        while (i % 2 == 0) {
            i = i / 2;
        }
        while (i % 3 == 0) {
            i = i / 3;
        }
        while (i % 5 == 0) {
            i = i / 5;
        }
        return i == 1;
    }*/
}
